'''
告警阀值设置
'''
from Lib.base.page_object.site_manage.alert_setting import AlertSetting
from Lib.base.page_object.site_manage.device_manage import DeviceManager
from Lib.base.page_object.site_manage.device_relevance_video_source import RelevanceVideoSource
from Lib.base.page_object.site_manage.report import Report
from Lib.base.page_object.site_manage.event import Event
from Lib.base.page_object.site_manage.task import Task
from selenium.webdriver.common.by import By
from Lib.common.ui_log import normal_log
import allure
from Lib.common.assert_class import AssertClass
import pytest


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestAlertSetting.assertClass = AssertClass(login)
    TestAlertSetting.alertSetting = AlertSetting(login)
    TestAlertSetting.deviceManage = DeviceManager(login)
    TestAlertSetting.relevanceVideoSource = RelevanceVideoSource(login)
    TestAlertSetting.report = Report(login)
    TestAlertSetting.event = Event(login)
    TestAlertSetting.task = Task(login)


@allure.feature("建设文明施工模块告警阀值设置")
class TestAlertSetting:

    @allure.story("告警阀值设置")
    @allure.title('新增告警阀值')
    def test_1_create_alert_setting(self):
        '''
        新增告警阀值
        :return:
        '''
        self.alertSetting.create_alert_setting()
        # 设置断言
        assert_arg = (By.XPATH, '//p[contains(text(), "新增成功")]')
        self.assertClass.make_assert_by_text(args=assert_arg, assert_data='新增成功', context='新增告警阀值')

    @allure.story("告警阀值设置")
    @allure.title('查看告警阀值列表')
    def test_2_alert_setting_list(self):
        '''
        告警阀值列表
        :return:
        '''
        normal_log().info('>>>>告警阀值列表测试开始')
        assert_dict = self.alertSetting.alert_setting_list()
        # 设置断言
        self.assertClass.make_assert_by_text(args=assert_dict['args'], assert_data=assert_dict['assert_data'])

    @allure.story("告警阀值设置")
    @allure.title('告警阀值列表查询、修改和删除')
    def test_3_setting_list_update_and_delete(self):
        '''
        告警阀值列表
        :return:
        '''
        assert_dict = self.alertSetting.setting_list_update_delete()
        # 设置断言
        self.assertClass.make_assert_by_text(args=assert_dict['args'], assert_data=assert_dict['assert_data'])

    @allure.story("设备管理")
    @allure.title('新建设备')
    def test_4_create_device(self):
        '''
        新建设备
        :return:
        '''
        assert_dict = self.deviceManage.create_device()
        # 设置断言
        self.assertClass.make_assert_by_text(args=assert_dict['args'], assert_data=assert_dict['assert_data'])

    @allure.story("设备管理")
    @allure.title('查询、删除和修改设备')
    def test_5_update_delete_device(self):
        '''
        删除和新建设备
        :return:
        '''
        assert_dict = self.deviceManage.device_update_delete()
        # 设置断言
        self.assertClass.make_assert_by_text(args=assert_dict['args'], assert_data=assert_dict['assert_data'])

    @allure.story("场所设备管理")
    @allure.title('设备关联视频源')
    def test_6_device_relevance_video_source(self):
        '''
        关联和取消关联设备
        :return:
        '''
        with allure.step("设备关联视频源"):
            self.relevanceVideoSource.relevance()
            assert_args = (By.XPATH, '//p[contains(text(), "关联成功")]')
            # 设置断言
            self.assertClass.make_assert_by_text(args=assert_args, assert_data="关联成功")
        with allure.step("取消设备关联视频源"):
            self.relevanceVideoSource.cancel_relevance()
            assert_args = (By.XPATH, '//p[contains(text(), "取消关联成功")]')
            # 设置断言
            self.assertClass.make_assert_by_text(args=assert_args, assert_data="取消关联成功")

    @allure.story("统计报表")
    @allure.title('工地筛选')
    def test_7_report_screen(self):
        '''
        按工程类型进行筛选
        :return:
        '''
        self.report.report_screen()
        assert_args = (By.XPATH, '//tbody//div[@title="房屋建筑"]')
        # 设置断言
        self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data="房屋建筑")

    @allure.story("统计报表")
    @allure.title('工地搜索')
    def test_8_report_search(self):
        '''
        按工程类型进行筛选
        :return:
        '''
        with allure.step("根据项目名称进行搜索"):
            self.report.report_search_by_name()
            assert_args = (By.XPATH, '//tbody//tr/td[1]//pre')
            # 设置断言
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data="项目")
        with allure.step("根据项目地址进行搜索"):
            self.report.report_search_by_address()
            assert_args = (By.XPATH, '//tbody//tr/td[2]//pre')
            # 设置断言
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data="上海市浦东新区")
        with allure.step("根据项目编码进行搜索"):
            self.report.report_search_by_encoding()
            assert_args = (By.XPATH, '//tbody//tr/td[3]//div')
            # 设置断言
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data="project")
        with allure.step("搜索错误的值"):
            self.report.report_search_by_error()
            assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
            # 设置断言
            self.assertClass.make_assert_by_text(args=assert_args, assert_data="暂无数据")

    @allure.story("违法事件")
    @allure.title('新建关联工地的事件')
    def test_9_create_event(self):
        '''
        新建关联工地的事件
        :return:
        '''
        self.event.create_event()
        assert_args = (By.XPATH, '//p[contains(text(), "事件上报成功")]')
        self.assertClass.make_assert_by_text(args=assert_args, assert_data='事件上报成功')

    @allure.story("违法事件")
    @allure.title('查看工地事件列表和事件详情')
    def test_10_event_list(self):
        '''
        新建关联工地的事件
        :return:
        '''
        with allure.step('查看事件列表是否为工地相关'):
            self.event.event_list()
            assert_args = (By.XPATH, '//tbody/tr/td[2]//span')
            self.assertClass.make_assert_by_value_in_elements_text_list(args=assert_args, assert_data='建设')
        with allure.step('查看事件详情是否关联工地'):
            self.event.event_search_and_detail()
            assert_args = (By.XPATH, '//div[@class="project_name_value"]/div')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='测试现建项目_请勿删除')

    @allure.story("执法任务")
    @allure.title("新建工地执法任务")
    def test_create_site_task(self, login):
        with allure.step('新建任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("工地执法任务筛选列表")
    def test_task_site_list(self):
        with allure.step('根据任务来源进行筛选'):
            self.task.task_list_by_source()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[8]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='系统新建')
        with allure.step('根据任务阶段进行筛选'):
            self.task.task_list_by_stage()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[6]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='受理')
        with allure.step('根据任务状态进行筛选'):
            self.task.task_list_by_status()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[7]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("工地执法任务列表排序")
    def test_site_task_sort(self, login):
        self.task.task_list_sort()
        # 设置断言
        assert_args1 = (By.XPATH, '//tbody//tr[1]/td[3]//span')
        assert_args2 = (By.XPATH, '//tbody//tr[2]/td[3]//span')
        self.assertClass.make_assert_by_compare_time(args1=assert_args1, args2=assert_args2)

    @allure.story("执法任务")
    @allure.title("工地执法任务搜索")
    def test_site_task_search(self, login):
        with allure.step('错误搜索'):
            self.task.task_search_error()
            # 设置断言
            assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='暂无数据')
        with allure.step('按任务编号和执法对象进行搜索'):
            self.task.task_search()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr[1]/td[4]//span')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='测试现建')

    @allure.story("执法任务")
    @allure.title("工地执法任务受理")
    def test_site_task_accept(self):
        with allure.step('执法任务受理'):
            self.task.task_detail_accept()
            assert_args = (By.XPATH, '//div[@title="已受理"]//div[text()="已受理"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已受理')

    @allure.story("执法任务")
    @allure.title("工地执法任务检查")
    def test_site_task_inspect(self):
        with allure.step('执法任务检查'):
            self.task.task_detail_inspect()
            assert_args = (By.XPATH, '//div[@title="已检查"]//div[text()="已检查"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已检查')

    @allure.story("执法任务")
    @allure.title("工地执法任务复查")
    def test_site_task_review(self):
        with allure.step('执法任务复查'):
            self.task.task_detail_review()
            assert_args = (By.XPATH, '//div[@title="已复查"]//div[text()="已复查"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已复查')

    @allure.story("执法任务")
    @allure.title("工地执法任务归档")
    def test_site_task_pigeonhole(self):
        with allure.step('执法任务归档'):
            self.task.task_detail_pigeonhole()
            assert_args = (By.XPATH, '//div[@title="已归档"]//div[text()="已归档"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已归档')

    @allure.story("执法任务")
    @allure.title("执法任务退单")
    def test_site_task_chargeback(self, login):
        with allure.step('新建执法任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已下达')
        with allure.step('执法任务检查'):
            self.task.task_chargeback()
            assert_args = (By.XPATH, '//div[@title="已退单"]//div[text()="已退单"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已退单')


if __name__ == '__main__':
    pytest.main(
        ['-v', '-s', '--reruns=1', '--reruns-delay=2',
         './alert_setting_test.py::TestAlertSetting'])
    # pytest.main(['-v', './alert_setting_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
